package com.ydd.base;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.ydd.R;
import com.ydd.constans.Constans;
import com.ydd.util.StatusBarColorUtil;
import com.ydd.widegt.HeaderView;

/**
 * Created by tao on 17-3-11.
 */

public class BaseActivity extends AppCompatActivity {
    protected String TAG;

    private HeaderView mHeadView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getComponentName().getClassName();
    }

    public void initHeaderView(String title, boolean showLeft) {
        mHeadView = (HeaderView) this.findViewById(R.id.header);
        mHeadView.showLeft(showLeft);
        mHeadView.showTitleView(title);
    }

    public void initHeaderView(String title) {
        mHeadView = (HeaderView) this.findViewById(R.id.header);
        mHeadView.showTitleView(title);
    }

    /**
     * 检查是否有指定权限
     */
    public boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获得指定权限
     */
    public void requetPermission(int code, String... permissions) {
        if (Build.VERSION.SDK_INT > 23) {
            ActivityCompat.requestPermissions(this, permissions, code);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case Constans.WRITE_READ_EXTERNAL_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doSDWritePermission();
                }
                break;
        }
    }

    public void doSDWritePermission() {

    }

    /**
     * 隐藏状态栏
     */
    public void hidedenStatusBar() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 改变状态栏的颜色
     */
    public void changeStatusBarColor(@ColorRes int colorId) {
        StatusBarColorUtil.setStatusBarColor(this, colorId);
    }


}
